Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

health check API 에서 db connection 확인 #314

Merged
merged 1 commit into from
Aug 3, 2023
Merged

Conversation

davin111
Copy link
Member

@davin111 davin111 commented Aug 2, 2023

https://wafflestudio.slack.com/archives/C0PAVPS5T/p1690977225130339

k8s liveness probe 가 호출하는 GET /health-check API 에서 db 연결도 체크하도록 함. 로컬에서 실제로 db 정상 연결될 때, 안 될 때 각각 200, 500 잘 반환하는 것 확인함.

@davin111 davin111 requested a review from Hank-Choi August 2, 2023 12:57
@davin111 davin111 self-assigned this Aug 2, 2023
@davin111 davin111 requested a review from a team as a code owner August 2, 2023 12:57

let router = ExpressPromiseRouter();

router.get('/health-check', (req, res) => res.sendStatus(200));
router.get('/health-check', (req, res) => {
if (mongoose.connection.readyState !== 1) {
Copy link
Member Author

@davin111 davin111 Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

밑에서 ping 하는데 이걸 굳이 왜 했지? 싶을 수 있는데.

애당초 최초 connection 부터 망가져 있는 경우에는, 밑에 mongoose.connection.db 쪽에서 TypeError: Cannot read property 'method' of undefined 에러가 발생하고, 이것이 읽기 좋지 않아 본질을 흐리기에 미리 체크하고 가는 것.

mongoose.connection.readyState의 경우에는 ping 과 달리 별도의 db 자원을 소모하지 않음.

@davin111 davin111 merged commit e80fdfe into develop Aug 3, 2023
1 check passed
@davin111 davin111 deleted the health-check-ping branch August 3, 2023 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant